<?php
namespace app\admin\controller;

use think\Controller;

class Nav extends Controller
{
    public function lst()
    {
        if (request()->isPost()) {
            $data = input('post.');
            foreach ($data['sort'] as $k => $v) {
                $data['sort'] = db('nav')->where(array('id' => $k))->update(['sort' => $v]);
            }
            $this->success('排序成功 ！');

        }
        $navRes = db('nav')->order('sort asc')->paginate(10);

        $this->assign([
            'navRes' => $navRes,

        ]);
        return view('list');
    }
    public function add()
    {
        if (request()->isPost()) {

            $data = input('post.');
            if ($data['nav_url'] && stripos($data['nav_url'], 'http://') === false) {
                $data['nav_url'] = 'http://' . $data['nav_url'];
            }

            //验证

            $add = db('nav')->insert($data);

            if ($add) {

                $this->success('添加导航成功 ！', 'lst');
            } else {

                $this->error('添加导航失败');
            }
            return;
        }

        return view();
    }
    public function edit()
    {

        if (request()->isPost()) {
            $data = input('post.');
            if ($data['nav_url'] && stripos($data['nav_url'], 'http://') === false) {
                $data['nav_url'] = 'http://' . $data['nav_url'];
            }

            $save = db('nav')->update($data);
            if ($save !== false) {

                $this->success('修改导航成功 ！', 'lst');
            } else {

                $this->error('修改导航失败');
            }
            return;
        }

        $id = input('id');

        $nav = db('nav')->find($id);

        $this->assign([
            'nav' => $nav,

        ]);

        return view();
    }
    public function del($id)
    {
        $del = db('nav')->delete($id);

        if ($del) {

            $this->success('删除导航成功 ！', 'lst');
        } else {

            $this->error('删除导航失败');
        }
    }

}
